//
//$Id: utils.cpp 722 2012-11-29 17:33:43Z andrew.tsyganov $
#include "stdafx.h"
#include "include/utils.h"
#include "include/subset.h"

using namespace std;

std::string OpenBracket(Brackets_Type const bt)
{
    switch (bt)
    {
        case PARENTHESES:  return "("; 
        case BRACKETS:     return "["; 
        case BRACES:       return "{"; 
        case LATEX_BRACES: return "\\{";
        default:           return "";
    }
}

std::string CloseBracket(Brackets_Type const bt)
{
    switch (bt)
    {
        case PARENTHESES:  return ")"; 
        case BRACKETS:     return "]"; 
        case BRACES:       return "}"; 
        case LATEX_BRACES: return "\\}";
        default:           return "";
    }
}

size_t Min(size_t a, size_t b, size_t c)
{
    if (b < c)
        if (a < b)
            return a;
        else
            return b;
    else
        if (a < c)
            return a;
        else
            return c;
}

std::string GetFileExtension(string const& filename)
{
    size_t pos = filename.rfind('.');

    if (pos == filename.npos) 
        return ""; 
    else
        return filename.substr(pos+1);
}

std::string CreatePath(string const& dir, string const& file)
{
    size_t n = dir.length();

    if ((dir[n-1] == '\\') || (dir[n-1] == '/'))
        return dir + file;

    size_t pos = dir.find_last_of("/\\");

    if (pos == dir.npos)
        return dir + "/" + file;

    if (dir[pos] == '\\')
        return dir + "\\" + file;
    else
        return dir + "/" + file;
}

void SplitFilename(string const& path, string& dir, string& file)
{
    size_t pos = path.find_last_of("/\\");

    dir  = path.substr(0, pos);
    file = path.substr(pos+1);
}

void SplitFilename(string const& path, string& dir, string& name, string& ext)
{
    SplitFilename(path, dir, name);
    
    size_t pos = name.find_last_of(".");
    
    if (pos == name.npos)
    {
        ext = "";
    }
    else
    {
        ext  = name.substr(pos+1);
        name = name.substr(0, pos);
    }
}

bool EquivalentRows(Bool_Matrix const& m, size_t const i, size_t const j) 
{
    for (size_t k = 0; k < m[i].size(); ++k)
        if (m[i][k] != m[j][k])
            return false;

    return true;
}

bool EquivalentColumns(Bool_Matrix const& m, size_t const i, size_t const j) 
{
    for (size_t k = 0; k < m.size(); ++k)
        if (m[k][i] != m[k][j])
            return false;

    return true;
}

bool AllOnesInRow(Bool_Matrix const& m, size_t const i, Subset const& s)
{
    for (Subset::enumerator en_k = s.first(); en_k < s.end(); ++en_k)
        if (m[i][*en_k] == 0)
            return false;

    return true;
}

bool AllOnesInColumn(Bool_Matrix const& m, size_t const j, Subset const& s)
{
    for (Subset::enumerator en_k = s.first(); en_k < s.end(); ++en_k)
        if (m[*en_k][j] == 0)
            return false;

    return true;
}

size_t OnesCount(Bool_Matrix const& m)
{
    size_t num = 0;

    for (size_t i = 0; i < m.size(); ++i)
        for (size_t j = 0; j < m[i].size(); ++j)
            if (m[i][j])
                ++num;

    return num;
}

//EOF!
